﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TravelLink.DAL;

namespace TravelLink.WebModel {
    public class Pagebase : System.Web.UI.Page, ISetNav, ISetSeo {

        #region  Property
        public OnlineUserInfo CurrentUser {
            get {
                return UserStoreFacade.Get();
            }
        }

        public void SetValidatorCode(string type, string value) {


            Session["TRAVEL_LINK_VALIDATOR_" + type] = value;

        }

        public string GetValidatorCode(string type) {
            return Session["TRAVEL_LINK_VALIDATOR_" + type] as string;
        }

        public virtual bool IsAjax { get { return false; } }



        private TravelLinkDBDataContext _db;
        private object _dbLock = new object();
        public TravelLinkDBDataContext DB {
            get {
                if (_db == null) {
                    lock (_dbLock) {
                        if (_db == null) {
                            _db = DataContextFactory.Create();
                        }
                    }
                }
                return _db;
            }
        }

        #endregion


        #region Event

        protected virtual void InitializeCode(string type) {

            string chkCode = string.Empty;

            //验证码的字符集，去掉了一些容易混淆的字符 
            char[] character = { '2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
            Random rnd = new Random();
            //生成验证码字符串 
            for (int i = 0; i < 4; i++) {
                chkCode += character[rnd.Next(character.Length)];
            }
            SetValidatorCode(type, chkCode);
        }


        protected override void OnInit(EventArgs e) {
            base.OnInit(e);
            if (!IsAjax) {
              
            }

        }

        protected override void OnPreRender(EventArgs e) {
            base.OnPreRender(e);
        }
        #endregion


        #region Helper Method

        private int _scriptFlag = 0;

        public void RegisterScript(string script) {

            this.ClientScript.RegisterStartupScript(this.GetType(), "Script_" + _scriptFlag, script, true);
            _scriptFlag++;
        }
        #endregion

        public void SetNav(Queue<NavItem> navs) {
            if (this.Master is ISetNav) {
                (this.Master as ISetNav).SetNav(navs);
            }
        }

        public void SetSeo(SEOInfo seo) {
            if (this.Master is ISetSeo) {
                (this.Master as ISetSeo).SetSeo(seo);
            }
        }
    }
}
